package com.zjitc.serviceedu.service.impl;

import com.zjitc.serviceedu.entity.EduWatchhistory;
import com.zjitc.serviceedu.entity.vo.WatchHistoryVO;
import com.zjitc.serviceedu.mapper.EduWatchhistoryMapper;
import com.zjitc.serviceedu.service.EduWatchhistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 赵诚挚
 * @since 2024-10-19
 */
@Service
public class EduWatchhistoryServiceImpl extends ServiceImpl<EduWatchhistoryMapper, EduWatchhistory> implements EduWatchhistoryService {
    @Autowired
    private EduWatchhistoryMapper watchHistoryMapper;
    @Override
    public void saveOrUpdateWatchHistory(Long userId,Long vid, Long courseId, String videoId, Integer lastPosition) {
        EduWatchhistory watchHistory = watchHistoryMapper.getWatchHistory(userId, videoId);
        if (watchHistory == null) {
            EduWatchhistory eduWatchhistory = new EduWatchhistory();
            eduWatchhistory.setUserId(userId); // 使用传入的 userId
            eduWatchhistory.setVid(vid);
            eduWatchhistory.setCourseId(courseId); // 使用传入的 courseId
            eduWatchhistory.setVideoId(videoId); // 使用传入的 videoId
            eduWatchhistory.setLastPosition(lastPosition);
            watchHistoryMapper.insertWatchHistory(eduWatchhistory); // 这里应使用 eduWatchhistory
        } else {
            // 如果已有历史记录，更新记录
            watchHistory.setLastPosition(lastPosition);
            watchHistoryMapper.updateWatchHistory(watchHistory);
        }
    }

    @Override
    public EduWatchhistory getWatchHistory(Long userId, String videoId) {
        return watchHistoryMapper.getWatchHistory(userId, videoId);
    }

    @Override
    public List<WatchHistoryVO> getWatchHistoryList(Long userId) {
        return watchHistoryMapper.getWatchHistoryList(userId);
    }


}
